<?php # Script 8.5 - register02.php
$page_title = '사용자 등록';
include('includes/header.html');
?>
	<div id="wrap">
		<div class="navbar navbar-inverse">
			<div class="navbar-inner">
				<div class="container">
					<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</a>
					<a class="brand" href="index.php">User Manager</a>
					<div class="nav-collapse collapse">
						<ul class="nav">
							<li class=""><a href="index.php">홈</a></li>
							<li class="active"><a href="register02.php">사용자 등록</a></li>
							<li class=""><a href="view_users02.php">사용자 목록</a></li>
							<li class=""><a href="password.php">비밀번호 변경</a></li>
							<li class=""><a href="#">link five</a></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
		<div class="container">
			<div class="page-header">
    	<!-- Start of the page-specific content. -->
<?php

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {
	
	require_once('./mysqli_connect.php'); // Connect to the db.

	$errors = array(); // Initialize an error array.

	// Check for a first name:
	if (empty($_POST['first_name'])) {
		$errors[] = 'You forgot to enter your first name.';
	} else {
		$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
	}

	// Check for a last name:
	if (empty($_POST['last_name'])) {
		$errors[] = 'You forgot to enter your last name.';
	} else {
		$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
	}	

	// Check for an email address:
	if (empty($_POST['email'])) {
		$errors[] = 'You forgot to enter your email address.';
	} else {
		$e = mysqli_real_escape_string($dbc, trim($_POST['email']));
	}

	// Check for unique email:
	$q = "select user_id from users where email='$e'";
	$r = @mysqli_query($dbc, $q);

	// Count the number of returned rows:
	$num = mysqli_num_rows($r);
	if($num > 0) { // If it already has same email, display error:
		$errors[] = "There is already same email address.";
	}

	// Check for a password and match against the confirmed password:
	if (!empty($_POST['pass1'])) {
		if ($_POST['pass1'] != $_POST['pass2']) {
			$errors[] = 'Your password did not match the confirmed password.';
		} else {
			$p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
		}
	} else {
		$errors[] = 'You forgot to enter your password.';
	}

	if (empty($errors)) { // If everything's OK.
		
		// Make the query:
		$q = "insert into users (first_name, last_name, email, pass, registration_date) values ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";
		$r = @mysqli_query($dbc, $q); // Run the query.
		if ($r) { // If it ran OK.
			
			// Print a message:
			echo '<h1>Thank you!</h1>
			<p>You are now registered. In Chapter 11 you will actually be able to log in!</p></p><br/></p>';

		} else { // If it did not run OK.

			// Public message:
			echo '<h1>System Error</h1>
			<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';

			// Debugging message:
			echo '<p>' . mysqli_error($dbc) . '<br/><br/>Query: ' . $q . '</p>';

		} // End of if ($r) IF.

		mysqli_close($dbc); // Close the database connection.

		// Include the footer and quit the script:
		include('includes/footer.html');
		exit();

	} else { // Report the errors.

		echo '<h1>Error!</h1>
		<p class="error">The following error(s) occurred:<br/>';
		foreach ($errors as $msg) { // Print each error.
			echo "- $msg<br/>\n";
		}
		echo '</p><p>Please try again.</p><p><br/></p>';

	}  // End of if (empty($errors)) IF.

	mysqli_close($dbc); // Close the database connection.

} // End of the main Submit conditional.
?>
<h1>사용자 등록</h1>
<form action="register02.php" method="post" class="form-horizontal">
	<fieldset>
		<div class="control-group">
			<label class="control-label">First Name:</label>
			<div class="controls">
				<input type="text" class="input-xlarge" name="first_name" size="15" maxlength="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">Last Name:</label>
			<div class="controls">
				<input type="text" class="input-xlarge" name="last_name" size="15" maxlength="40" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">Email Address:</label>
			<div class="controls">
				<input type="text" class="input-xlarge" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">Password:</label>
			<div class="controls">
				<input type="password" class="input-xlarge" name="pass1" size="10" maxlength="20">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">Confirm Password:</label>
			<div class="controls">
				<input type="password" class="input-xlarge" name="pass2" size="10" maxlength="20">
			</div>
		</div>
		<div class="form-actions">
            <button type="submit" class="btn btn-primary btn-large">Register</button>
            <input type="hidden" name="submitted" value="TRUE">
        </div>
	</fieldset>
</form>
<?php
include('includes/footer.html');
?>